﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Interface;
using LY.DataAccess.Repository;
using LY.Entity.PageSearch;
using LY.Entity;
using LY.Entity.Entities;


namespace LY.Business.ServiceImpl
{
    public class ModuleService : IModuleService
    {
        ModuleRepository _repo;
        public ModuleService()
        {
            _repo = new ModuleRepository();
        }

        /// <summary>
        /// 模块列表
        /// </summary>
        /// <param name="view"></param>
        /// <returns></returns>
        public PagedList<S_Module> QueryModuleList(PageView view)
        {
            return _repo.QueryModuleList(view);
        }

        /// <summary>
        /// 新用户有权限的模块列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<S_Module_EX> QueryNewRoleModuleList()
        {
            return _repo.QueryNewRoleModuleList();
        }

        /// <summary>
        /// 用户有权限的模块列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<S_Module_EX> QueryRoleModuleList(int roleid)
        {
            return _repo.QueryRoleModuleList(roleid);
        }

        /// <summary>
        /// 用户有权限的模块列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<S_Module> QueryUserModuleList(int id)
        {
            return _repo.GetUserAccessList(id);
        }

        public List<S_Module> GetUpperModuleList(int? id)
        {
            return _repo.GetUpperModuleList(id);
        }

        /// <summary>
        /// 保存模块信息
        /// </summary>
        /// <param name="inves"></param>
        /// <returns></returns>
        public int SaveModule(S_Module module)
        {
            string oldpath = "";
            string newpath = "";
            int moduleSequence = 0;
            if (module.Sequence != null)
            {
                moduleSequence = Convert.ToInt32(module.Sequence);
            }
            if (module.ID <= 0)
            {
                //新增
                //获取父级资料
                int ParentID = Convert.ToInt32(module.ParentID);
                S_Module parentmodule = _repo.GetModule(ParentID);
                module.Layer = parentmodule.Layer + 1;
                module.Path = parentmodule.Path + Convert.ToString(module.ID + moduleSequence * 10000 + 100000000) + ',';
                return (int)_repo.Insert(module);
            }
            else
            {
                //编辑
                oldpath = module.Path;
                //获取父级资料
                int ParentID = Convert.ToInt32(module.ParentID);
                S_Module parentmodule = _repo.GetModule(ParentID);
                module.Layer = parentmodule.Layer + 1;
                newpath = parentmodule.Path + Convert.ToString(module.ID + moduleSequence * 10000 + 100000000) + ',';

                UpdateSequence(oldpath, newpath);
                module.Path = newpath;
                return (int)_repo.Update(module);

            }
        }

        public int UpdateSequence(string oldpath, string newpath)
        {
            return _repo.UpdateSequence(oldpath, newpath);
        }

        /// <summary>
        /// 删除模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteModule(int id)
        {
            return _repo.DeleteModule(id);
        }

        /// <summary>
        /// 检索模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public S_Module GetModule(int? id)
        {
            return _repo.GetModule(id);
        }

        /// <summary>
        /// 检索模块信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<S_Module> GetModuleSub(int? id)
        {
            return _repo.GetModuleSub(id);
        }

    }
}
